MFC获取WindowsDPI

获取操作系统版本

OSVERSIONINFO osvi; 
ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); 
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 
GetVersionEx(&osvi);

根据osvi的两个变量dwMajorVersion,dwMinorVersion来区分版本

Win8.1系统以下,不包括Win8.1获取方法

// pixels in screen dc 
HDC hdc = GetDC(NULL); 
if (hdc) 
{ 
    g_dpiX = GetDeviceCaps(hdc, LOGPIXELSX);//每英寸逻辑像素数 水平 
    g_dpiY = GetDeviceCaps(hdc, LOGPIXELSY); //每英寸逻辑像素数 垂直 
    ReleaseDC(NULL, hdc); 
}

Win8.1以上系统,包括Win8.1获取方法

HMONITOR hMonitor; 
POINT pt; 
HRESULT hr = E_FAIL; 
pt.x = 1; 
pt.y = 1; 
hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST); 
hr = GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &g_dpiX, &g_dpiY);

设置缩放比例

UINT m_nScaleFactor; 
void SetScale(__in UINT iDPI) 
{ 
    m_nScaleFactor = MulDiv(iDPI, 100, 96); //MulDiv(a,b,c) a*b/c 
}

关于API GetDpiForMonitor function说明区分操作系统的原因

Minimum supported client Windows 8.1 [desktop apps only]

Minimum supported server Windows Server 2012 R2 [desktop apps only] Header ShellScalingAPI.h

本页共42段,1105个字符,1317 Byte(字节)